﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EditPaymentSchedule.aspx.cs" Inherits="CRMWeb.Pages.AM.EditPaymentSchedule" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Edit Payment Schedule</title>
    <link id="newtheme" rel="stylesheet" type="text/css" href="/EasyUI/Themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="/EasyUI/Themes/icon.css" />
    <link rel="stylesheet" type="text/css" href="/Css/CrmStyle.css" />
    <script type="text/javascript" src="/Scripts/jquery-1.8.0.min.js"></script>
    <script type="text/javascript" src="/EasyUI/jquery.easyui.min.js"></script>
</head>
<body>
    <table style="width: 100%;">
        <tr>
            <td colspan="4" style="text-align: right;">
                <span id="lnkbtnSaveRepayment"><a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-edit'" onclick="editPaymentSchedule()">Edit</a></span>
                <span id="lnkbtnFresh"><a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-reload'" onclick="">Fresh</a></span>
                <span id="lnkbtnPaymentSchedule"><a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="">Save</a></span>
            </td>
        </tr>
    </table>
    <div data-options="region:'west',split:true" style="float: left; width: 35%; padding-left: 20px;">
        <div id="p" class="easyui-panel" title="Edit Schedule" style="width: 210px; height: 500px;">
            <input type="hidden" id="hdnbankid1" />
            <input type="hidden" id="hdnbankid2" />
            <table style="width: 100%;">
                <tr>
                    <td style="vertical-align: top; width: 100%;">
                        <div class="topform_section">
                            <div class="ftitle">Change All Payments</div>
                            <div class="fitem">
                                <label>Payment Amount:</label>
                                <input id="txtEditRepayment" style="width: 150px;" value="0.00" class="easyui-numberbox" data-options="min:0,precision:2" name="Repayment" />
                            </div>
                            <div class="fitem">
                                <label>Payment Frequency:</label>
                                <input class="easyui-combobox" id="txtEditFreq" style="width: 150px;" name="Freq"
                                    data-options="url:'/WebServices/AM/LoanForm.ashx?method=GetAllPaymentPeriod',
				                                    valueField:'PaymentPeriod',
				                                    textField:'PaymentPeriod',
                                                    editable:false,
				                                    panelHeight:'auto'" />
                            </div>
                            <div class="fitem">
                                <label>Starting Date:</label><br />
                                <input id="txtEditFirstPaymentDue" name="firtspaymentdue" class="easyui-datebox" data-options="formatter:formatLoanDate" style="width: 90px;" />
                            </div>
                            <div class="fitem">
                                <span id="lnkbtnSubmitAllPayments"><a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="ChangeAllPayments()">Submit</a></span>
                            </div>
                            <br />
                            <br />
                            <div class="ftitle">Add a Single Payment</div>
                            <div class="fitem">
                                <label>Payment Amount:</label>
                                <input id="txtSingleRepayment" style="width: 150px;" value="0.00" class="easyui-numberbox" data-options="min:0,precision:2" name="Repayment" />
                            </div>
                            <div class="fitem">
                                <label>Payment Date:</label><br />
                                <input id="txtSingleFirstPaymentDue" name="singlefirtspaymentdue" class="easyui-datebox" data-options="formatter:formatLoanDate" style="width: 90px;" />
                            </div>
                            <div class="fitem">
                                <label>Replace a Dishonour?</label>
                                <input id="txtReplaceDishonour" style="width: 150px;" class="easyui-validatebox" name="Freq" />
                            </div>
                            <div class="fitem">
                                <span id="lnkbtnSubmitSinglePayments"><a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="AddSinglePayment()">Submit</a></span>
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <table id="dgPaymentSchedule" title="PAYMENT SCHEDULE" class="easyui-datagrid" style="width: 400px; height: auto;"
        data-options="url:'/WebServices/AM/Repayment.ashx?method=RePaymentScheduleSelect&LoanID='+getQuerystring('LoanID', ''),
        region:'center',
        singleSelect: true,
        onClickRow: onClickRow" 
        rownumbers="true" fitcolumns="true" singleselect="true">
        <thead>
            <tr>
                <th field="date" width="40" data-options="formatter:formatLoanDate">Due Date</th>
                <th field="day" width="40">Day</th>
                <th field="amount" width="50">Payment</th>
            </tr>
        </thead>
    </table>
    <div id="dlgPaymentSchedule" class="easyui-dialog" style="width: 350px; height: 220px; padding: 10px 20px"
        closed="true" buttons="#dlg-buttons">
        <div class="ftitle">Payment Schedule Information</div>
        <form id="fmPaymentSchedule" method="post" novalidate>
            <div class="fitem">
                <label style="width: 130px;">Due Date:</label>
                <input id="txtPaymentSchedule" name="PaymentSchedule" class="easyui-datebox" data-options="formatter:formatLoanDate" style="width: 90px;" />
                <input id="hdnPaymentScheduleID" value="" style="display: none" />
            </div>
            <div class="fitem">
                <label style="width: 130px;">Payment:</label>
                <input id="txtAmount" style="width: 110px;" value="0.00" class="easyui-numberbox" data-options="min:0,precision:2" name="Amount" />
            </div>
            <div class="fitem">
                <label style="width: 130px;">Balance:</label>
                <input id="txtPaymentBalance" style="width: 110px;" value="0.00" class="easyui-numberbox" data-options="min:0,precision:2" name="Balance" />
            </div>
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="savePaymentSchedule()">Save</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlgPaymentSchedule').dialog('close')">Cancel</a>
    </div>
    <script type="text/javascript">
        var url;
        function newPaymentSchedule() {
            $('#dlgPaymentSchedule').dialog('open').dialog('setTitle', 'New Payment Schedule');
            $('#fmPaymentSchedule').form('clear');
            $('#txtPaymentSchedule').val('');
            $('#txtAmount').numberbox('setValue', '0.00');
            $('#txtPaymentBalance').numberbox('setValue', '0.00');
            $('#hdnPaymentScheduleID').val('');
            url = '/WebServices/AM/Repayment/PaymentScheduleAdd';
        }

        $(document).ready(function () {
            $('#newtheme').attr('href', '/EasyUI/Themes/' + getQuerystring('Theme', 'Default') + '/easyui.css');
        });

        var editIndex = undefined;
        function endEditing() {
            if (editIndex == undefined) { return true }
            if ($('#dgPaymentSchedule').datagrid('validateRow', editIndex)) {
                var ed = $('#dgPaymentSchedule').datagrid('getEditor', { index: editIndex, field: 'productid' });
                var productname = $(ed.target).combobox('getText');
                $('#dgPaymentSchedule').datagrid('getRows')[editIndex]['productname'] = productname;
                $('#dgPaymentSchedule').datagrid('endEdit', editIndex);
                editIndex = undefined;
                return true;
            } else {
                return false;
            }
        }

        function onClickRow(index) {
            if (editIndex != index) {
                if (endEditing()) {
                    $('#dgPaymentSchedule').datagrid('selectRow', index)
                    .datagrid('beginEdit', index);
                    editIndex = index;
                } else {
                    $('#dgPaymentSchedule').datagrid('selectRow', editIndex);
                }
            }
        }

        function editPaymentSchedule() {
            $('#dgPaymentSchedule').edatagrid({
                
            });
        }
        function ChangeAllPayments() {
            /*Get Numeric Value*/
            var EditRepayment = $("#txtEditRepayment").numberbox('getValue');
           
            /*Get DropDown Value*/
            var EditFreq = $('#txtEditFreq').combobox('getValue');
         
            /*Get Date Value*/
            var EditFirstPaymentDue = $('#txtEditFirstPaymentDue').datebox('getValue');
            if (EditFirstPaymentDue != '')
                EditFirstPaymentDue = DefaultformatLoanDate($('#txtEditFirstPaymentDue').datebox('getValue'));
          
            /*set value to numeric if it is blank*/
            if (EditRepayment == '')
                EditRepayment = '0';
            
            $.ajax({
                url: '/WebServices/AM/LoanForm.ashx?method=SimpleInterestPaymentScheduleByRepayment',
                contentType: 'application/json; charset=utf-8',
                data: { "LoanID": getQuerystring('LoanID', ''), "repaymentAmount": EditRepayment, "paymentperiod": EditFreq, "firstPaymentDate": EditFirstPaymentDue },
                success: function (data) {
                    if (data == '1') {
                        $('#dgPaymentSchedule').datagrid('reload');
                    }
                }
            });
        }

        function AddSinglePayment() {
            /*Get Numeric Value*/
            var SingleRepayment = $("#txtSingleRepayment").numberbox('getValue');

            /*Get Date Value*/
            var SingleFirstPaymentDue = $('#txtSingleFirstPaymentDue').datebox('getValue');
            if (SingleFirstPaymentDue != '')
                SingleFirstPaymentDue = DefaultformatLoanDate($('#txtSingleFirstPaymentDue').datebox('getValue'));

            /*set value to numeric if it is blank*/
            if (SingleRepayment == '')
                SingleRepayment = '0';

            //Create Payment Schedule
            $.ajax({
                url: '/WebServices/AM/LoanForm.ashx?method=AddSinglePayment',
                contentType: 'application/json; charset=utf-8',
                data: { "LoanID": getQuerystring('LoanID', ''), "SingleRepayment": SingleRepayment, "SingleFirstPaymentDue": SingleFirstPaymentDue },
                success: function (data) {
                    if (data == '1') {
                        $('#dgPaymentSchedule').datagrid('reload');
                    }
                }
            });
        }

        function deletePaymentSchedule() {
            var row = $('#dgPaymentSchedule').datagrid('getSelected');
            if (row) {
                $.ajax({
                    url: '/WebServices/AM/Repayment.ashx',
                    type: 'POST',
                    data: {
                        method: 'PaymentScheduleDelete',
                        args: { paymentscheduleid: row.paymentscheduleid }
                    },
                    success: function (data) {
                        $('#dgPaymentSchedule').datagrid('reload');
                        alert('Delete Data Successfully.');
                    }
                });
            }
        }
        function savePaymentSchedule() {
            var strPaymentSchedule = $('#txtPaymentSchedule').datebox('getValue');
            if (strPaymentSchedule != '')
                strPaymentSchedule = DefaultformatLoanDate($('#txtPaymentSchedule').datebox('getValue'));
            var strAmount = $("#txtAmount").numberbox('getValue');
            var strBalance = $("#txtPaymentBalance").numberbox('getValue');
            var ResultID = $('#hdnPaymentScheduleID').val();
            if (ResultID != '') {
                if (strPaymentSchedule != '') {
                    $.ajax({
                        url: '/WebServices/AM/Repayment.ashx',
                        type: 'POST',
                        data: {
                            method: 'PaymentScheduleEdit',
                            args: { paymentscheduleid: ResultID, PaymentSchedule: strPaymentSchedule, Amount: strAmount, Balance: strBalance }
                        },
                        success: function (data) {
                            if (data == 1) {
                                $('#dlgPaymentSchedule').dialog('close');
                                $('#dgPaymentSchedule').datagrid('reload');
                                alert('Update Data Successfully.');
                            }
                            else {
                                alert('PaymentSchedule already exist.');
                            }
                        }
                    });
                }
            }
            else {
                if (strPaymentSchedule != '') {
                    $.ajax({
                        url: '/WebServices/AM/Repayment.ashx',
                        type: 'POST',
                        data: {
                            method: 'PaymentScheduleAdd',
                            args: { PaymentSchedule: strPaymentSchedule, Amount: strAmount, LoanID: getQuerystring('LoanID', '') }
                        },
                        success: function (data) {
                            if (data == 1) {
                                $('#dlgPaymentSchedule').dialog('close');
                                $('#dgPaymentSchedule').datagrid('reload');
                                alert('Insert Data Successfully.');
                            }
                            else {
                                alert('PaymentSchedule already exist.');
                            }
                        }
                    });
                }
            }
        }

        function formatLoanDate(value, row) {
            if (value == null)
                return value;
            var date = new Date(value);
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            var d = date.getDate();
            return (d < 10 ? ('0' + d) : d) + '/' + (m < 10 ? ('0' + m) : m) + '/' + y;
        }

        function DefaultformatLoanDate(value) {
            dArr = value.split('/');
            return dArr[1] + '/' + dArr[0] + '/' + dArr[2];
        }

        //get querystring value
        function getQuerystring(key, defaultValue) {
            if (defaultValue == null) defaultValue = "";
            key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
            var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
            var qs = regex.exec(window.location.href);
            if (qs == null)
                return defaultValue;
            else {
                return qs[1];
            }
        }

    </script>
</body>
</html>
